<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@ include file="/sys/include/header.jsp" %>
<jsp:useBean id="roleList"  class="java.util.ArrayList" scope="request"/>
<jsp:useBean id="menuList"  class="java.util.ArrayList" scope="request"/>
<jsp:useBean id="menuAllList"  class="java.util.ArrayList" scope="request"/>
<html>
<head> 
<title>权限管理</title>
<link href="sys//style01/css/table.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="sys/js/menutree/drighttree.js"></script>
<link rel="StyleSheet" href="sys/css/menutree/dtree.css" type="text/css" /> 
</head>  
<script type="text/javascript"> 
 
 	var menuId=new Array();
 	var menuPid=new Array();
 	var checkedList=new Array();
 	
	function dogroup(form){
		var list=form.select;
		var chosevalue=list.value;
		if(chosevalue!=0){
			form.roleId.value=chosevalue;
			form.submit();
		} else{
			form.action="system/getRoleList.action";
			form.submit();
		}
	}

	function checkedmenu(){
		var num=checkedList;			
		for(var i=0;i<num.length;i++){
			eval("document.form1."+num[i]+".checked=true");
		}
	}
	
	//保存权限树
	function saverule(form){ 
		var id=new Array(form.elements.length);
		var result="";
		for (var i=0;i<form.elements.length;i++){
			var e = form.elements[i];
		    if (e.checked&&e.name!=undefined&&e.name!='roleId'&&e.name!='select') 
				result+=e.name+",";
		}
		
		if(form.roleId.value=="roleId"||form.roleId.value=="")
			alert("没有选择要修改的权限组，不能进行保存！");
		else{
			form.action="system/manageMenu.action?roleId="+form.roleId.value+"&result="+result;
			form.submit();
		}		
	}
	
	
	//选择全部
	 function checkedall(form){
	 	for (var i=0;i<form.elements.length;i++){
	    	var e = form.elements[i];
	       	e.checked=true;
	   	}
	 }
	 
	 //取消全部
	 function uncheckedall(form){
	 for (var i=0;i<form.elements.length;i++){
	    	var e = form.elements[i];
	       	e.checked=false;
	   	}
	 }
	//联动设置
	function parent(el,e2){ 
		var id=el; 
		var pid=e2;
		if(eval("document.form1."+id+".checked==true")&&id!='a0'){
			eval("document.form1."+pid+".checked=true"); 
			for(var k=0;k<menuId.length;k++){ 
				if(menuId[k]==pid){ 
					id=pid; 
					pid=menuPid[k];
					parent(id,pid); 
				} 
			} 
		}else{
			for(var k=0;k<menuId.length;k++){
				if(menuPid[k]==id){
					var tid=menuId[k];
					eval("document.form1."+tid+".checked=false");
					var tpid=menuPid[k];
					parent(tid,tpid);					
				}
			}
		}
	} 
 
</script>
 <body class="ridgeBody">
<table width="100%">
	<tr><td>
	<form name="form1" method="post" action="system/getMenuList.action">
	<input type="hidden" value="<c:out value="${roleId}"/>" name="roleId" id="roleId" />
		<fieldset align='left' style="width:500"> 
		  <legend>
		  	<font  style="cursor:hand;" size="2"><img src="sys/images/getpic.gif"> 请选择查询条件</font>
		  </legend>
		  <table id="tab4" style="display:block;" class=datatable width=500 cellpadding='0' cellspacing='0' border='1' bordercolorDark='white' bordercolorLight='#BEBEBE' >
			<TR><TD height="24"  valign="top" class="dataTitle"></TD></TR>
			<TR>
				<TD width="302" valign="top" >选择角色:
					<select name="select"  onChange="dogroup(this.form)">
						<option value="0">请选择</option>
						<c:forEach items="${roleList}" var="role">
						<option value="${role.commonCode}" <c:if test="${role.commonCode==roleId}">selected</c:if>>${role.commonValue}</option>	
						</c:forEach>
					</select>
			  	</TD>
			</TR>
			<TR>
				<td width="602" valign="top" >				
				<c:if test="${!empty  menuAllList}">				
					<div name="tree" class="dtree"><script type="text/javascript">
						d = new dTree('d');
						d.add('a0','-1','系统功能菜单');	
						<c:forEach items="${menuAllList}" var="menuAll">
							d.add('${menuAll.menuId}','${menuAll.parentId}','${menuAll.menuName}','','${menuAll.menuDesc}','','','');
							menuId[menuId.length]	=	'${menuAll.menuId}';
 							menuPid[menuPid.length]	=	'${menuAll.parentId}';
						</c:forEach>
						document.write(d);
						d.openAll();
						<c:forEach items="${menuList}" var="menu">
							checkedList[checkedList.length] = '${menu.menuId}';
						</c:forEach>
						checkedmenu();
					</script>						
					</div>					
				</c:if>
			  	</td>
			  	</tr><tr>
			  	<TD width="602" valign="top" >
			  		<c:if test="${!empty  menuAllList}">
						<input type="button" onClick="saverule(form1)" value="保存角色"/>
						<input type="button" onClick="checkedall(form1)" value="选择全部"/>
						<input type="button" onClick="uncheckedall(form1)" value="取消全部"/>
					</c:if>
			  	</TD>
			</TR>
			</form>
		  </table>
		  
		</fieldset>
 	</form> 
	</td></tr>
</table>
</body>
</HTML>
